package org.example.studentmanager.controller;

import org.example.studentmanager.common.utils.R;
import org.example.studentmanager.entity.RegisterRequest;
import org.example.studentmanager.entity.Student;
import org.example.studentmanager.entity.Teacher;
import org.example.studentmanager.entity.User;
import org.example.studentmanager.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    // 新的用户注册接口
    @PostMapping("/insert")
    public String registerUser(@RequestBody RegisterRequest request) {
        User user = request.getUser();
        Student student = request.getStudent();
        Teacher teacher = request.getTeacher();
        return adminService.registerUser(user, student, teacher);
    }

    /***/
    @GetMapping("/query/user/{id}")
    public R<User> getUserById(@PathVariable("id") Integer id) {
        return R.success(adminService.getUserById(id));
    }

    // 修改用户信息接口
    @PostMapping("/update")
    public void updateUser(@RequestBody User user) {
        adminService.updateUser(user);
    }

    // 查询所有用户信息接口
    @GetMapping("/users")
    @ResponseBody
    public List<User> findAllUsers() {
        return adminService.findAllUsers();
    }

//    // 删除用户接口
//    @GetMapping("/users/delete/{user_id}")
//    public int deleteUser(@PathVariable("user_id") Integer userId) {
//        return adminService.deleteUser(userId);
//    }

    // 根据真实姓名模糊查询用户列表
    @GetMapping("/users/query")
    public List<User> findUsersByRealName(@RequestParam(value = "name", required = false) String name) {
        return adminService.findUsersByRealName(name);
    }

    // 新增：根据姓名删除用户接口
    @GetMapping("/users/deleteByName")
    public int deleteUserByName(@RequestParam("name") String name) {
        return adminService.deleteUserByName(name);
    }

    @GetMapping("/users/deleteById")
    public int deleteUserById(@RequestParam("id") Integer userId) {
        return adminService.deleteUserById(userId);
    }
}
